<?php
/**
 * Classe controladora de Envio de Newsletter
 *
 */
class PC4_Controlador_Crud_NewslettersEnvios extends TICE_Controlador_Crud
{
	function init(){
		parent::init();
		$this->uploads = array('anexos' => array(), 'corpo_email_imagem' => array('validacoes' => array('IsImage')));
		
    	if ($this->modeloEmails == null) {
			$this->modeloEmails = new PC4_Db_Table(array(Zend_Db_Table::NAME => $this->tabelaEmails, Zend_Db_Table::PRIMARY => 'email'));
		}
		
		//$this->mAnexo = new Anexo();
		
		$info = $this->modeloEmails->info();
		$this->tabelaEmails = $info['name'];
		
		$rsEnvios =  $this->modeloEmails->fetchAll();
		$rsEmails = $this->modeloEmails->fetchAll();
		$emails = array(); $emailsSelecionados = array(); $emailsPorEnvio = array();
		
		foreach($rsEmails as $r){
			$emails['Todos'][$r->email]->nome = $r->email;
		}
		
		foreach($rsEnvios as $r){
			$emailsSelecionados[$r->id_newsletter_envios][] = $r->email;
			$emailsPorEnvio[$r->id_newsletter_envios][$r->email] = $r->status;
		}
		
		$this->view->emails = $emails;
		$this->view->emailsPorEnvio = $emailsPorEnvio;
		$this->view->emailsSelecionados = $emailsSelecionados;
		
		$this->mEmailsNewsletters = new Zend_Db_Table('newsletters_emails');
		
	}
	
    public function lista($where = null, $order = null, $count = null, $offset = null){
    	if($this->usuario->role != 'master'){
    		$where = 'ne.id_newsletter_clientes = ' . $this->usuario->id_newsletter_clientes;
    	}
    	parent::lista($where, $order, $count, $offset);
    }
	
    public function persisteInsere($registro){
		$emails = $registro['emails'];
		unset($registro['clientes']);
		unset($registro['emails']);
    	$idInserido = parent::persisteInsere($registro);
    	if(!empty($emails)){
    		$this->persisteEmailsRelacionados($idInserido, $emails);
    	}
//    	parent::persisteInsere($registro);
    }
    
    public function persisteEdita($registro, $id){
    	//TODO: REFATORAR COLOCANDO NO MODELO
    	$emails = $registro['emails'];
//var_dump($emails);die;
		unset($registro['clientes']);
		unset($registro['emails']);
    	if(!empty($emails)){
    		$this->persisteEmailsRelacionados($id, $emails);
    	}
    	parent::persisteEdita($registro, $id);
    }
    
	/**
	 * Funcao que persiste os emails na tabela relacionada checando duplicida
	 * @param unknown_type $idFk
	 * @param unknown_type $emails
	 */
    public function persisteEmailsRelacionados($idFk, $emails){
		$rsEmailsCadastrados = $this->modeloEmails->getLista('id_newsletter_envios = ' . $idFk);
		$emailsCadastrados = array();
		foreach($rsEmailsCadastrados as $r){
    		$emailsCadastrados[] = $r->email;
		}
		
		// compara os arrays e separa os novos emails e os que devem ser deletados
		$novosEmails = array_diff($emails, $emailsCadastrados);
		$emailsParaDeletar = array_diff($emailsCadastrados, $emails);

		if(!empty($emailsParaDeletar)){
			$this->modeloEmails->delete("id_newsletter_envios = " . $idFk . " AND email IN ('" . implode("','", $emailsParaDeletar) . "')");

		}
		if(!empty($novosEmails)){
			foreach((array)$novosEmails as $email){
	    		$bean['id_newsletter_envios'] = $idFk;
	    		$bean['email'] = $email;
		    	$this->modeloEmails->insert($bean);
	    	}
		}	
    }
     	
	public function persisteApaga($id){
    	$this->apagaRelacionados($id);
    	parent::persisteApaga($id);
    }
    
    public function apagaRelacionados($idFk){
		$where = $this->modeloEmails->getAdapter()->quoteInto('id_newsletter_envios = ?', $idFk);
		$total = $this->modeloEmails->delete($where);
    }
}
